package cn.java79.quiz.service;

import cn.java79.quiz.dao.QuizMapper;
import cn.java79.quiz.entity.Questions;
import cn.java79.quiz.entity.Quiz;
import cn.java79.quiz.entity.Quizsel;
import cn.java79.quiz.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class QuizService {
    public static final QuizService ME=new QuizService();
    public static final int QUIZ_SIZE=10;
    public long generateQuiz(Long userid, String operator, long secid){
        SqlSession s= MybatisUtil.get();
        try {
            QuizMapper mapper = s.getMapper(QuizMapper.class);
            List<Long> longs = mapper.chooseForQuiz(secid, QUIZ_SIZE);
            Quiz q = new Quiz();
            q.setOperator(operator);
            q.setScores("10");
            q.setTitle(userid + "的自测");
            q.setOpenid(secid);
            q.setStartdate(new Date());

            mapper.add(q);//返回主键id
            long quizid = q.getId();
            for (Long qid : longs) {
                Quizsel qs = new Quizsel();
                qs.setQid(qid);
                qs.setQuizid(quizid);
                mapper.addSel(qs);
            }
            //return mapper.getQues(quizid);
            return quizid;
        }finally {
            MybatisUtil.close(s);
        }
    }
    public List<Questions> getQuizs(Long quizid){

        SqlSession s= MybatisUtil.get();
        try {
            QuizMapper mapper = s.getMapper(QuizMapper.class);

            return mapper.getQues(quizid);
        }finally {
            MybatisUtil.close(s);
        }
    }

    public static void main(String[] args) {

    }
}
